﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibFriesland_Campina_CRM;
using System.Collections;

public partial class UserControls_UCAttendanceTrackingDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["by_na"] = Request.QueryString["by_na"].ToString();
        ViewState["by_shop"] = Request.QueryString["by_shop"].ToString();
        ViewState["by_shift"] = Request.QueryString["by_shift"].ToString();
        ViewState["dateTracking"] = Request.QueryString["dateTracking"].ToString();
        databindTracking(ViewState["by_na"].ToString(), ViewState["by_shop"].ToString(), ViewState["by_shift"].ToString(), ViewState["dateTracking"].ToString());
    }

    //Lien ket du lieu
    private void databindTracking(string pBy_na, string pBy_shop, string pBy_shift, string pDatetracking)
    {
        //Hien thi thong tin NA
        BizNA na = BizNA.getNabyID(pBy_na);
        lbNa_name.Text = na.name;
        lbBy_na.Text = na.na_charid;

        //Hien thi thong tin Shop
        BizShop shop = BizShop.getShopbyID(pBy_shop);
        lbShop_name.Text = shop.name;

        //Hien thi thong tin ca lam viec
        BizShift shift = BizShift.getShiftbyID(pBy_shift);
        lbShift.Text = shift.name;

        DateTime dateTracking = Convert.ToDateTime(Thuvien.myConvertStringToShortDate(pDatetracking));
        List<BizAttendanceTracking> adt = BizAttendanceTracking.getFullAttendanceTrackingbyNA(pBy_na, dateTracking, pBy_shop, pBy_shift);
        hyperViewShopGPS.NavigateUrl = String.Format("../MapWithClickablePushpins.aspx?shop={0}&type={1}", pBy_shop, "viewShopPoint");

        //Dau gio
        BizAttendanceTracking starttimeTracking = adt.Find(p => p.by_time_point == "1");
        displayTrackingTime(starttimeTracking, lbStarttime_gps, lbStarttime_phone, lbStarttime_system);
        displayTrackingStatus(starttimeTracking, lbStarttime_gps_status, lbStarttime_ontime_status, hyperStarttime_Latlng,pBy_na);
        displayTrackingActionbySup(starttimeTracking, lbStarttime_action, lbStarttime_ontime_status, lbStarttime_gps_status, hyperStarttime_action, "1");

        //Giua gio
        BizAttendanceTracking middletimeTracking = adt.Find(p => p.by_time_point == "2");
        displayTrackingTime(middletimeTracking, lbMiddletime_gps, lbMiddletime_phone, lbMiddletime_system);
        displayTrackingStatus(middletimeTracking, lbMiddletime_gps_status, lbMiddletime_ontime_status, hyperMiddletime_Latlng, pBy_na);
        displayTrackingActionbySup(middletimeTracking, lbMiddletime_action, lbMiddletime_ontime_status, lbMiddletime_gps_status, hyperMiddletime_action, "2");

        //Cuoi gio
        BizAttendanceTracking endtimeTracking = adt.Find(p => p.by_time_point == "3");
        displayTrackingTime(endtimeTracking, lbEndtime_gps, lbEndtime_phone, lbEndtime_system);
        displayTrackingStatus(endtimeTracking, lbEndtime_gps_status, lbEndtime_ontime_status, hyperEndtime_Latlng, pBy_na);
        displayTrackingActionbySup(endtimeTracking, lbEndtime_action, lbEndtime_ontime_status, lbEndtime_gps_status, hyperEndtime_action, "3");
    }

    //Hien thi thong tin thoi gian cham cong
    private void displayTrackingTime(BizAttendanceTracking adt, Label lbTime_gps, Label lbTime_phone, Label lbTime_system)
    {
        if (adt != null)
        {
            DateTime? datetimeGPS = adt.datetime_gps;
            if (datetimeGPS != null)
                lbTime_gps.Text = Convert.ToDateTime(datetimeGPS).ToString("dd/MM/yyyy HH:mm:ss");
            else
                lbTime_gps.Text = "-";
            lbTime_phone.Text = Convert.ToDateTime(adt.datetime_phone).ToString("dd/MM/yyyy HH:mm:ss");
            lbTime_system.Text = Convert.ToDateTime(adt.datetime_system).ToString("dd/MM/yyyy HH:mm:ss");
        }
        else
        {
            lbTime_gps.Text = "N/A";
            lbTime_phone.Text = "N/A";
            lbTime_system.Text = "N/A";
        }
    }

    //Hien thi thong tin trang thai cham cong
    private void displayTrackingStatus(BizAttendanceTracking adt, Label lbGps_status, Label lbOntime_status, HyperLink hyperLatlng, string pBy_na)
    {
        if (adt != null)
        {
            if (adt.latitude != 0 || adt.longitude != 0) //Neu lay duoc toa do GPS
            {
                if (adt.getLatlngStatus() == true)
                {
                    lbGps_status.ForeColor = System.Drawing.Color.Blue;
                    lbGps_status.Text = "Có";
                }
                else
                {
                    lbGps_status.ForeColor = System.Drawing.Color.Red;
                    lbGps_status.Text = "Không";
                }

                hyperLatlng.NavigateUrl = String.Format("~/MapWithClickablePushpins.aspx?latGPS={0}&longGPS={1}&type={2}", adt.latitude, adt.longitude, "viewTrackingPoint");
            }
            else
            {
                lbGps_status.ForeColor = System.Drawing.Color.Red;
                lbGps_status.Text = "N/A";
                hyperLatlng.Visible = false;
            }

            int timeStatus = adt.getTimeStatus();
            if (timeStatus == 1)
            {
                lbOntime_status.ForeColor = System.Drawing.Color.Blue;
                lbOntime_status.Text = "Có";
            }
            else
            {
                lbOntime_status.ForeColor = System.Drawing.Color.Red;
                lbOntime_status.Text = "Không";
            }
        }
        else
        {
            lbGps_status.Text = "N/A";
            lbOntime_status.Text = "N/A";
            hyperLatlng.Visible = false;
        }
    }

    //Hien thi thong tin action cua Supervisor
    private void displayTrackingActionbySup(BizAttendanceTracking adt, Label lbAction, Label lbOntime_status, Label lbGPS_status, HyperLink hyperAction, string pBy_time_point)
    {
        string temp = String.Format("openPopupSupMonitoring('{0}','{1}','{2}','{3}',{4})", ViewState["by_na"].ToString(), ViewState["by_shift"].ToString(), ViewState["by_shop"].ToString(), ViewState["dateTracking"].ToString(), pBy_time_point.ToString());
        hyperAction.Attributes.Add("onclick", temp);
        if (adt != null)
        {
            if (adt.comment_monitoring != "")
            {
                lbAction.ForeColor = System.Drawing.Color.Blue;
                lbAction.Text = "Có";
            }
            else
            {
                if (lbOntime_status.Text == "Không" || lbGPS_status.Text == "Không")
                    lbAction.ForeColor = System.Drawing.Color.Red;
                lbAction.Text = "Không";
            }
        }
        else
            lbAction.Text = "N/A";
    }
}